Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Reflection

Namespace Config

    Public Class ConfigSettings

        Dim mCfgPath As String

        Public Function ReadSetting(ByVal key As String) As String

            Try
                Dim doc As XmlDocument
                Dim node As XmlNode
                doc = loadConfigDocument()

                'retrieve appSettings node
                node = doc.SelectSingleNode("//appSettings")

                If node Is Nothing Then
                    MsgBox("Could not find <appSettings> section in the app.config file", MsgBoxStyle.Exclamation, "Section Missing")
                    Return Nothing
                Else
                    'select the 'add' element that contains the key
                    Dim elem As XmlElement = CType(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)), XmlElement)

                    Return elem.GetAttribute("value")

                End If

            Catch exc As Exception
                'add in error handling here
                Return Nothing
            End Try

        End Function

        Public Sub WriteSetting(ByVal key As String, ByVal value As String)

            Dim doc As XmlDocument = loadConfigDocument()

            Dim node As XmlNode = doc.SelectSingleNode("//appSettings")

            If node Is Nothing Then

                Throw New InvalidOperationException("appSettings section not found in config file.")

            Else

                Try

                    Dim elem As XmlElement = node.SelectSingleNode(String.Format("//add[@key='{0}']", key))

                    If Not elem Is Nothing Then

                        elem.SetAttribute("value", value)

                    Else

                        elem = doc.CreateElement("add")
                        elem.SetAttribute("key", key)
                        elem.SetAttribute("value", value)
                        node.AppendChild(elem)

                    End If

                    doc.Save(getConfigFilePath())

                Catch

                    Throw

                End Try

            End If

        End Sub

        Public Sub RemoveSetting(ByVal key As String)

            Dim doc As XmlDocument = loadConfigDocument()

            Dim node As XmlNode = doc.SelectSingleNode("//appSettings")

            Try

                If node Is Nothing Then

                    Throw New InvalidOperationException("appSettings section not found in config file.")

                Else

                    node.RemoveChild(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)))
                    doc.Save(getConfigFilePath())

                End If

            Catch e As NullReferenceException

                Throw New Exception(String.Format("The key {0} does not exist.", key), e)

            End Try

        End Sub

        Private Function loadConfigDocument() As XmlDocument

            Dim doc As XmlDocument = Nothing

            Try

                doc = New XmlDocument()
                doc.Load(getConfigFilePath())
                Return doc

            Catch e As System.IO.FileNotFoundException

                Throw New Exception("No configuration file found.", e)

            End Try

        End Function

        Private Function getConfigFilePath() As String

            If mCfgPath.Length > 0 Then
                Return mCfgPath
            Else
                Return Assembly.GetExecutingAssembly().Location + ".config"
            End If

        End Function

        Public Sub SetConfigFile(ByVal val As String)
            mCfgPath = val
        End Sub

        Public Sub New(ByVal cfgpath As String)

            mCfgPath = cfgpath

        End Sub

    End Class

End Namespace